package com.addplus.townmall.server.api.mapper.town;

import com.addplus.townmall.server.api.model.town.SpikeActive;
import com.addplus.townmall.server.api.model.town.expand.SpikeActiveExpand;
import com.addplus.townmall.server.api.model.town.expand.SpikeActiveShopExpand;
import com.addplus.townmall.server.api.utils.BaseAddMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 商城秒杀活动表 Mapper 接口
 * </p>
 *
 * @author zhangjiehang
 * @since 2018-08-27
 */
public interface SpikeActiveMapper extends BaseAddMapper<SpikeActive> {


    @Select("SELECT\n" +
            " s.shop_id,\n" +
            " a.status,\n" +
            " a.begin_date,\n" +
            " a.end_date,\n" +
            " s.line_price,\n" +
            " s.sale_price,\n" +
            " s.stock,\n" +
            " s.limit_count,\n" +
            " s.sale_count \n" +
            "FROM\n" +
            " spike_active_shop s\n" +
            " RIGHT JOIN spike_active a ON a.id = s.spike_active_id \n" +
            " AND a.is_deleted = 0 \n" +
            "WHERE\n" +
            " s.shop_id = #{shopProductId} \n" +
            " AND s.is_deleted = 0")
    @Results({
            @Result(column = "shop_id", property = "spikeActiveShop.shop_id"),
            @Result(column = "line_price", property = "spikeActiveShop.line_price"),
            @Result(column = "sale_price", property = "spikeActiveShop.sale_price"),
            @Result(column = "stock", property = "spikeActiveShop.stock"),
            @Result(column = "limit_count", property = "spikeActiveShop.limit_count"),
            @Result(column = "sale_count", property = "spikeActiveShop.sale_count")
    })
    SpikeActiveExpand getSpikeActiveShopProduce(@Param("shopProductId") Integer shopProductId);

    @Select("SELECT\n" +
            " id,\n" +
            " name,\n" +
            " status,\n" +
            " begin_date,\n" +
            " end_date,\n" +
            " img_pate \n" +
            "FROM\n" +
            " spike_active \n" +
            "WHERE\n" +
            " is_deleted = 0 and\n" +
            " online_status = 1 and\n" +
            " status !=2\n" +
            " AND (begin_date >= now() or end_date > now())\n" +
            " ORDER BY status desc, begin_date \n" +
            " limit 2")
    @Results({
            @Result(property = "id", column = "id"
            ),
            @Result(property = "spikeActiveShopList", column = "id",
                    many = @Many(select = "com.addplus.townmall.server.api.mapper.town.SpikeActiveShopMapper.getSpikeActiveShopByActive")
            )
    })
    List<SpikeActiveExpand> getSpikeActiveMain(@Param("spikeDate") LocalDateTime spikeDate);


    @Select("SELECT\n" +
            " s.id,\n" +
            " s.shop_id,\n" +
            " s.shop_name,\n" +
            " s.shop_image,\n" +
            " s.sort,\n" +
            " a.status,\n" +
            " a.name,\n" +
            " a.img_pate,\n" +
            " a.begin_date,\n" +
            " a.end_date,\n" +
            " s.line_price,\n" +
            " s.sale_price,\n" +
            " s.stock,\n" +
            " s.limit_count,\n" +
            " s.sale_count \n" +
            "FROM\n" +
            " spike_active_shop s\n" +
            " left JOIN spike_active a ON a.id = s.spike_active_id \n" +
            " where s.is_deleted = 0 \n" +
            " ORDER BY s.gmt_modified desc ")
    @Results({
            @Result(column = "id", property = "spikeActiveShop.id"),
            @Result(column = "shop_id", property = "spikeActiveShop.shopId"),
            @Result(column = "shop_name", property = "spikeActiveShop.shopName"),
            @Result(column = "shop_image", property = "spikeActiveShop.shopImage"),
            @Result(column = "line_price", property = "spikeActiveShop.linePrice"),
            @Result(column = "sale_price", property = "spikeActiveShop.salePrice"),
            @Result(column = "stock", property = "spikeActiveShop.stock"),
            @Result(column = "limit_count", property = "spikeActiveShop.limitCount"),
            @Result(column = "sale_count", property = "spikeActiveShop.saleCount")
    })
    List<SpikeActiveExpand> getAllSpikeActiveShop(IPage page);

    @Select("SELECT\n" +
            " s.shop_id,\n" +
            " s.shop_name,\n" +
            " s.shop_image,\n" +
            " s.sort,\n" +
            " a.status,\n" +
            " a.name,\n" +
            " a.img_pate,\n" +
            " a.begin_date,\n" +
            " a.end_date,\n" +
            " s.line_price,\n" +
            " s.sale_price,\n" +
            " s.stock,\n" +
            " s.limit_count,\n" +
            " s.sale_count \n" +
            "FROM\n" +
            " spike_active_shop s\n" +
            " left JOIN spike_active a ON a.id = s.spike_active_id \n" +
            " where s.is_deleted = 0 \n" +
            " and s.spike_active_id = #{spike_active_id} \n" +
            " ORDER BY s.gmt_modified desc ")
    @Results({
            @Result(column = "id", property = "spikeActiveShop.id"),
            @Result(column = "shop_id", property = "spikeActiveShop.shopId"),
            @Result(column = "shop_name", property = "spikeActiveShop.shopName"),
            @Result(column = "shop_image", property = "spikeActiveShop.shopImage"),
            @Result(column = "line_price", property = "spikeActiveShop.linePrice"),
            @Result(column = "sale_price", property = "spikeActiveShop.salePrice"),
            @Result(column = "stock", property = "spikeActiveShop.stock"),
            @Result(column = "limit_count", property = "spikeActiveShop.limitCount"),
            @Result(column = "sale_count", property = "spikeActiveShop.saleCount")
    })
    List<SpikeActiveExpand> getSpikeActiveShopBySpikeActiveId(IPage page, @Param("spike_active_id") Integer spike_active_id);

    @Select("SELECT MAX(end_date) FROM spike_active WHERE is_deleted=0")
    Date getLastTime();
}
